package net.yunzhanyi.client.controller;

import io.swagger.annotations.ApiOperation;
import net.yunzhanyi.client.annotation.CheckLogin;
import net.yunzhanyi.client.annotation.CheckWebLogin;
import net.yunzhanyi.client.constant.AccountConstant;
import net.yunzhanyi.client.constant.UserConstant;
import net.yunzhanyi.client.domain.dto.ClientLoginUser;
import net.yunzhanyi.client.domain.vo.SexType;
import net.yunzhanyi.client.domain.vo.UserVo;
import net.yunzhanyi.client.service.ClientAccountService;
import net.yunzhanyi.client.service.ClientUserService;
import net.yunzhanyi.client.utils.AuthUtils;
import net.yunzhanyi.common.core.utils.StringUtils;
import net.yunzhanyi.common.redis.service.RedisService;
import net.yunzhanyi.common.web.domain.AjaxResult;
import net.yunzhanyi.common.web.util.DataMaskingUtils;
import net.yunzhanyi.generic.domain.model.ClientAccount;
import net.yunzhanyi.generic.domain.model.ClientUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author bestct
 * @date 2022/10/1
 * @type 类
 */
@Controller
public class UserController {
    @Autowired
    private ClientUserService clientUserService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ClientAccountService clientAccountService;

    @CheckWebLogin
    @GetMapping("/profile")
    public String profile(HttpServletRequest request, Model model) {
        ClientLoginUser loginUser = (ClientLoginUser) request.getAttribute("loginUser");
        ClientUser clientUser = clientUserService.getUserById(loginUser.getUid());
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(clientUser, userVo);
        userVo.setJoinTime(clientUser.getCreatedTime());
        model.addAttribute("clientUser", userVo);
        List<SexType> sexTypeList = new ArrayList<>();
        sexTypeList.add(new SexType("未知", (short) 0));
        sexTypeList.add(new SexType("男", (short) 1));
        sexTypeList.add(new SexType("女", (short) 2));
        model.addAttribute("sexTypeList", sexTypeList);
        model.addAttribute("userSilde", "profile");
        return "profile";
    }

    @CheckWebLogin
    @GetMapping("/account")
    public String account(HttpServletRequest request, Model model) {
        ClientLoginUser loginUser = (ClientLoginUser) request.getAttribute("loginUser");
        ClientAccount account = clientAccountService.findAccountByAid(loginUser.getAid());
        String desensitizePhone = DataMaskingUtils.mobilePhone(account.getPhone());
        String email = DataMaskingUtils.email(account.getEmail());
        model.addAttribute("phone", desensitizePhone);
        model.addAttribute("email", email);
        model.addAttribute("userSilde", "account");
        return "account";
    }

    @ApiOperation(value = "绑定手机")
    @CheckLogin
    @ResponseBody
    @PostMapping("/api/bind/phone")
    public AjaxResult bindPhone(
            @RequestParam String phone) {

        if (!StringUtils.isMobile(phone)) {
            return AjaxResult.error(AccountConstant.PHONE_ERROR);
        }
        long aid = AuthUtils.getAid();
        if (!clientAccountService.checkPhoneUnique(phone)) {
            return AjaxResult.error(AccountConstant.PHONE_NOT_UNIQUE);
        }
        clientAccountService.bindPhone(aid, phone);
        return AjaxResult.success();
    }

    @ApiOperation(value = "绑定邮箱")
    @CheckLogin
    @ResponseBody
    @PostMapping("/api/bind/email")
    public AjaxResult bindEmail(
            @RequestParam String email) {

        if (!StringUtils.isEmail(email)) {
            return AjaxResult.error(AccountConstant.EMAIL_ERROR);
        }
        long aid = AuthUtils.getAid();
        if (!clientAccountService.checkEmailUnique(email)) {
            return AjaxResult.error(AccountConstant.EMAIL_NOT_UNIQUE);
        }
        clientAccountService.bindEmail(aid, email);
        return AjaxResult.success();
    }


    @ApiOperation(value = "修改密码")
    @CheckLogin
    @PostMapping("/api/change/password")
    @ResponseBody
    public AjaxResult changePassword(@RequestParam String newPassword) {
        long aid = AuthUtils.getAid();
        if (StringUtils.isEmpty(newPassword) || newPassword.length() < AccountConstant.MIN_PASSWORD_LENGTH) {
            return AjaxResult.error(AccountConstant.PASSWORD_ERROR);
        }

        clientAccountService.changePassword(aid, newPassword);
        return AjaxResult.success();
    }


    @CheckLogin
    @PostMapping("/api/save/user")
    @ResponseBody
    private AjaxResult saveUser(@RequestBody UserVo user) {
        System.out.println(user);
        ClientUser clientUser = new ClientUser();
        BeanUtils.copyProperties(user, clientUser);
        long aid = AuthUtils.getAid();
        clientUser.setAid(aid);
        clientUser.setModifiedTime(System.currentTimeMillis());
        if (clientUserService.checkNickNameUnique(clientUser)) {
            clientUserService.saveUser(clientUser);
            redisService.setCacheMapValue(AuthUtils.getRedisTokenKey(), "nickName", clientUser.getNickName());
            redisService.setCacheMapValue(AuthUtils.getRedisTokenKey(), "sex", clientUser.getNickName());
            redisService.setCacheMapValue(AuthUtils.getRedisTokenKey(), "signature", clientUser.getNickName());
        } else {
            return AjaxResult.error(UserConstant.NICKNAME_ERROR);
        }
        return AjaxResult.success();
    }
}
